;SLIDE Main Library 

 INCLUDE "libraries.inc"

 CreateLibrary Main,Quit,Jump,Load,IfEqu,IfNEqu,IfGreater,IfLess,Exec,Slide,Return,Copy,LoopToZero,LoopToNum


 IFDEF __Exec
_Exec_:
       call   LdHLIndConvVal
       jp     (hl)
 ENDIF

 IFDEF __IfGreater
_IfGreater_:
       call   LdDEHLIndConvVal
       call   LdHLIndConvVal
       B_CALL CpHLDE
       ret    c
       jp     SkipNextCmdRet
 ENDIF

 IFDEF __IfLess
_IfLess_:
       call   LdDEHLIndConvVal
       call   LdHLIndConvVal
       ex     de,hl
       B_CALL CpHLDE
       ret    c
       jp     SkipNextCmdRet
 ENDIF


 IFDEF __IfNEqu
_IfNEqu_:
       call   LdDEHLIndConvVal
       call   LdHLIndConvVal
       B_CALL CpHLDE
       ret    nz
       jp     SkipNextCmdRet
 ENDIF

 IFDEF __IfEqu
_IfEqu_:
       call   LdDEHLIndConvVal
       call   LdHLIndConvVal
       B_CALL CpHLDE
       ret    z
       jp     SkipNextCmdRet
 ENDIF

 IFDEF __Return
 IFNDEF __Quit
 DEFINE __Quit
 ENDIF
_Return_:
       ld     a,(SlideDepth)
       or     a
       jr     z,_Quit_
       dec    a
       ld     (SlideDepth),a
       ld     l,a
       ld     h,0
       add    hl,hl
       ld     de,SlideStack
       add    hl,de
       B_CALL LdHLInd
       ld     (SlidePtr),hl
       ret
 ENDIF

 IFDEF __Quit
_Quit_:
       res    textInverse,(iy+textFlags)
       jp     DoQuit
 ENDIF

 IFDEF __LoopToNum
_LoopToNum_:
       push   hl
       call   LdDEHLIndConvVal
       inc    hl
       inc    hl                   ;Points to Jump to
       inc    hl
       inc    hl                   ;Points to Inc value
       call   LdHLIndConvVal       ;HL = inc value
       add    hl,de                ;HL = new value
       pop    bc
       push   bc
       ld     a,(bc)
       ld     e,a
       inc    bc
       ld     a,(bc)
       ld     d,a                  ;DE = Variable to increment
       push   hl
       call   LoadToVar
       pop    de                   ;DE = new value
       pop    hl                   ;HL->var to increment
       inc    hl
       inc    hl                   ;HL->loop until
       ld     c,(hl)
       inc    hl
       ld     b,(hl)               ;BC = Loop Until
       inc    hl
       B_CALL LdHLInd              ;HL = Jump To
       push   hl
       ld     h,b
       ld     l,c
       call   ConvVal16Bit
       or     a
       sbc    hl,de
       pop    hl
       ret    c
       ret    z
       ld     (SlidePtr),hl
       jp     SkipThisNextRet
 ENDIF


 IFDEF __LoopToZero
_LoopToZero_:
       ld     e,(hl)
       inc    hl
       ld     d,(hl)
       inc    hl
       push   hl
       push   de
       call   ConvVal16BitDE
       dec    de
       pop    hl
       ex     de,hl
       push   hl
       call   LoadToVar
       pop    de
       pop    hl
       call   LdHLIndConvVal
       ld     a,d
       or     e
       ret    z
       ld     (SlidePtr),hl
       jp     SkipThisNextRet
 ENDIF


 IFDEF __Jump
_Jump_:
       call   LdHLIndConvVal
       ld     (SlidePtr),hl
       jp     SkipThisNextRet
 ENDIF

 IFDEF __Copy
_Copy_:
       ld     e,(hl)
       inc    hl
       ld     d,(hl)
       inc    hl
       B_CALL LdHLInd
;       bit    7,h
;       jr     z,CopyError
       ex     de,hl
       jp     LoadToVar
;CopyError:
;       ld     hl,CopyErrorText
;       jp     ErrorH
;CopyErrorText:
;       db     "Copy Destination"
;       db     "Not a Var",0
 ENDIF              

 IFDEF __Load
_Load_:
       ld     e,(hl)
       inc    hl
       ld     d,(hl)
       inc    hl
;       bit    7,d
;       jr     z,LoadError
       B_CALL LdHLInd
       call   LoadToVar
       ret
;LoadError:
;       ld     hl,LoadErrorText
;       jp     ErrorH
;LoadErrorText:
;       db     "Load Destination"
;       db     "Not a  Var",0

 ENDIF     

 IFDEF __Slide
_Slide_:
       B_CALL LdHLInd
       ex     de,hl
       ld     a,(SlideDepth)
;       cp     MaxSlideDepth
;       jr     z,SlideError
       ld     l,a
       ld     h,0
       add    hl,hl
       ld     bc,SlideStack
       add    hl,bc
       ld     bc,(SlidePtr)
       ld     (hl),c
       inc    hl
       ld     (hl),b
       inc    a
       ld     (SlideDepth),a
       ld     (SlidePtr),de
       jp     SkipThisNextRet
;SlideError:
;       ld     hl,SlideErrorText
;       jp     ErrorH
;SlideErrorText:
;       db     "Slide Depth at  "
;       db     "Maximum",0
 ENDIF

MainNext:

 IFDEF _S_Quit
       db     2
 ENDIF
 IFDEF _S_Jump
       db     4
 ENDIF
 IFDEF _S_Load
       db     6
 ENDIF
 IFDEF _S_IfEqu
       db     6
 ENDIF
 IFDEF _S_IfNEqu
       db     6
 ENDIF
 IFDEF _S_IfGreater
       db     6
 ENDIF
 IFDEF _S_IfLess
       db     6
 ENDIF
 IFDEF _S_Exec
       db     4
 ENDIF
 IFDEF _S_Slide
       db     4
 ENDIF
 IFDEF _S_Return
       db     2
 ENDIF
 IFDEF _S_Copy
       db     6
 ENDIF
 IFDEF _S_LoopToZero
       db     6
 ENDIF
 IFDEF _S_LoopToNum
       db     10
 ENDIF


